/**
 * 
 */
package cn.com.xf.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import cn.com.xf.dao.TblOaSoftDAO;
import cn.com.xf.dao.TblOaSoftTypeDAO;
import cn.com.xf.domain.TblOaSoft;
import cn.com.xf.domain.TblOaSoftType;
import cn.com.xf.service.NoticeService;
import cn.com.xf.service.SoftService;
import cn.com.xf.web.security.UserInfo;

/**
 * @author Administrator
 * 
 */
@Service
public class SoftServiceImpl implements SoftService {
	@Autowired
	private TblOaSoftDAO coreFileDAO;
	@Autowired
	private TblOaSoftTypeDAO coreFileTypeDAO;
	@Autowired
	private NoticeService noticeService;

	@Override
	public List<TblOaSoft> select(int begin, int pageSize, String orderByField, String orderByMode, String fileType,String center) {
		if(fileType!=null&&fileType.equals("18")){
			return coreFileDAO.selectBJAndOrder(begin, pageSize, orderByField, orderByMode, fileType, center);
		}
		List<TblOaSoft> list = coreFileDAO.selectAndOrder(begin, pageSize, orderByField, orderByMode, fileType);
		return list;
	}

	@Override
	public int selectCount(String fileType, String center) {
		if(fileType!=null&&fileType.equals("18")){
			return coreFileDAO.selectBJCount(fileType,center);
		}
		return coreFileDAO.selectCount(fileType);
	}

	@Override
	public int insert(TblOaSoft coreFile,String centerId) {
		coreFileDAO.insert(coreFile,centerId);
		
		BigDecimal big = BigDecimal.valueOf(18);
		Integer fileId = coreFile.getFileId();
		if (0==big.compareTo(coreFile.getTypeId())){
			String centerSite = "";
			if("769000".equals(centerId)||"10000".equals(centerId)){
				String toZB = null!=coreFile.getToZB()?coreFile.getToZB():"";
				String toZJ = null!=coreFile.getToZJ()?coreFile.getToZJ():"";
				String toFJ = null!=coreFile.getToFJ()?coreFile.getToFJ():"";
				if(!toZB.equals("1")&&!toZJ.equals("1")&&!toFJ.equals("1")){
					return -1;
				}
				if(toZB.equals("1")){
					centerSite = "769000";
					coreFileDAO.insertSoftMapping(centerSite,fileId);
				}
				if(toZJ.equals("1")){
					centerSite = "570000";
					coreFileDAO.insertSoftMapping(centerSite,fileId);
				}
				if(toFJ.equals("1")){
					centerSite = "591000";
					coreFileDAO.insertSoftMapping(centerSite,fileId);
				}
			}else {
				centerSite = centerId;
				coreFileDAO.insertSoftMapping(centerSite,fileId);
			}
		}
		
		return 0;
	}

	@Override
	public TblOaSoft selectById(Integer id) {
		return coreFileDAO.selectByPrimaryKey(id);
	}

	@Override
	public int update(TblOaSoft coreFile) {
		coreFileDAO.updateByPrimaryKeyWithBLOBs(coreFile);
		
		BigDecimal big = BigDecimal.valueOf(18);
		Integer fileId = coreFile.getFileId();
		if (0==big.compareTo(coreFile.getTypeId())){
			//查询员工所属一级中心站点
			UserInfo user = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			String siteId = user.getSiteId();
			String centerId = "";
			if("10000".equals(siteId)){
				centerId = "10000";
			}else{
				centerId = noticeService.selectOfCenter(siteId);
			}
			
			if("769000".equals(centerId)||"10000".equals(centerId)){
				List<String> regions = selectRegions(fileId);
				//判断公告是浙江的福建的还是总部的
				String hasZJSoft = "";
				String hasZBSoft = "";
				String hasFJSoft = "";
				String oldCenterId = "";
				String centerSite = "";
				for(String id:regions){
					if("570000".equals(id)){
						hasZJSoft = "1";
					}else if("769000".equals(id)){
						hasZBSoft = "1";
						oldCenterId = id;
					}else if("591000".equals(id)){
						hasFJSoft = "1";
					}else if("571002".equals(id)){
						hasZBSoft = "1";
						oldCenterId = id;
					}
				}
				String toZB = null!=coreFile.getToZB()?coreFile.getToZB():"";
				String toZJ = null!=coreFile.getToZJ()?coreFile.getToZJ():"";
				String toFJ = null!=coreFile.getToFJ()?coreFile.getToFJ():"";
				if(!toZB.equals("1")&&!toZJ.equals("1")&&!toFJ.equals("1")){
					return -1;
				}
				//检查总部可见性
				if(toZB.equals("1")){
					if(!hasZBSoft.equals("1")){
						String oldCenterSite = coreFileDAO.selectOldCenterId(fileId);
						if("570000".equals(oldCenterSite)||"10000".equals(oldCenterSite)||"591000".equals(oldCenterSite)){
							oldCenterSite = "769000";
						}
						centerSite = oldCenterSite;
						coreFileDAO.insertSoftMapping(centerSite,fileId);
					}
				}else{
					if(hasZBSoft.equals("1")){
						coreFileDAO.deleteSoftMappingByCenterId(oldCenterId,fileId);
					}
				}
				if(toZJ.equals("1")){
					if(!hasZJSoft.equals("1")){
						centerSite = "570000";
						coreFileDAO.insertSoftMapping(centerSite,fileId);
					}
				}else{
					if(hasZJSoft.equals("1")){
						centerSite = "570000";
						coreFileDAO.deleteSoftMappingByCenterId(centerSite,fileId);
					}
				}
				if(toFJ.equals("1")){
					if(!hasFJSoft.equals("1")){
						centerSite = "591000";
						coreFileDAO.insertSoftMapping(centerSite,fileId);
					}
				}else{
					if(hasFJSoft.equals("1")){
						centerSite = "591000";
						coreFileDAO.deleteSoftMappingByCenterId(centerSite,fileId);
					}
				}
				return 1;
			}
		}
		return 1;
	}

	@Override
	public List<TblOaSoftType> selectCoreFileTypeList() {
		return coreFileTypeDAO.selectCoreFileTypeList();
	}

	@Override
	public int selectCount(String fileType, String fileName, String publicMan,
			String createDateBegin, String createDateEnd, String center) {
		if(fileType==null||fileType.equals("")){
			int otherCount = 0;
			int bjCount = 0;
			bjCount = coreFileDAO.selectBJCount("18", fileName, publicMan, createDateBegin, createDateEnd, center);
			otherCount =  coreFileDAO.selectOtherCount(fileType, fileName, publicMan, createDateBegin, createDateEnd, center);
			return bjCount+otherCount;
		}
		if(fileType!=null&&fileType.equals("18")){
			return coreFileDAO.selectBJCount(fileType, fileName, publicMan, createDateBegin, createDateEnd, center);
		}
		return coreFileDAO.selectCount(fileType, fileName, publicMan, createDateBegin, createDateEnd);
	}

	@Override
	public List<TblOaSoft> select(int begin, int pageSize, String orderByField,
			String orderByMode, String fileType, String fileName,
			String publicMan, String createDateBegin, String createDateEnd, String center) {
		if(fileType==null||fileType.equals("")){
			return coreFileDAO.selectAllAndOrder(begin, pageSize, orderByField, orderByMode, fileType, fileName, publicMan, createDateBegin, createDateEnd, center);
		}
		
		if(fileType!=null&&fileType.equals("18")){
			return coreFileDAO.selectBJAndOrder(begin, pageSize, orderByField, orderByMode, fileType, fileName, publicMan, createDateBegin, createDateEnd, center);
		}
		
		return coreFileDAO.selectAndOrder(begin, pageSize, orderByField, orderByMode, fileType, fileName, publicMan, createDateBegin, createDateEnd);
	}

	@Override
	public TblOaSoftType selectCoreFileTypeById(BigDecimal id) {
		return coreFileTypeDAO.selectByPrimaryKey(id);
	}

	@Override
	public int deleteById(Integer id) {
		coreFileDAO.deleteByPrimaryKey(id);
		return coreFileDAO.deleteSoftMappingByPrimaryKey(id);
	}

	@Override
	public List<String> selectRegions(Integer id) {
		List<String> list = coreFileDAO.selectRegionsById(id);
		return list;
	}


}
